package com.hepinghospital.user.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.text.TextUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: lzp
 * @CreateDate: 2022/7/7 9:14
 * @UpdateRemark: 更新说明
 */
public class HarmonyUtil {

    /**
     * 判断是否鸿蒙系统
     *
     * @param context
     * @return
     */
    public static boolean isHarmony(Context context) {
        try {
            //判断是否存在鸿蒙系统的运行库，非Harmony系统或者EMUI系统的会找不到这个类而抛异常，
            //可以快速排除掉其他的Android系统
            Class<?> accountAbilityClass = Class.forName("ohos.aafwk.ability.Ability");

            //再判断是否有纯净模式的activity，来进一步区分是Harmony系统或者是EMUI系统
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setPackage("com.huawei.systemmanager");
            intent.setComponent(new ComponentName("com.huawei.systemmanager",
                    "com.huawei.systemmanager.puremode.PureModeActivity"));
            PackageManager packageManager = context.getPackageManager();
            List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            return list.size() > 0;

        } catch (Throwable e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取鸿蒙纯净模式状态
     * 0：开启，1：关闭
     *
     * @param context
     * @return
     */
    public static int readPureModeState(Context context) {
        int result = 1;
        if (!isHarmony(context)) {
            return result;
        }
        try {
            if (context != null) {
                result = Settings.Secure.getInt(context.getContentResolver(), "pure_mode_state", 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取华为系统版本名称
     *
     * @return String  返回值：harmony
     */
    public static String osBandName() {
        try {
            Class clz = Class.forName("com.huawei.system.BuildEx");
            return (String) clz.getMethod("getOsBrand").invoke(clz);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取华为系统版本号
     *
     * @return 版本号
     */
    public static String harmonyOsv() {
        return getProp("hw_sc.build.platform.version", "");
    }

    private static String getProp(String property, String defaultValue) {
        try {
            Class spClz = Class.forName("android.os.SystemProperties");
            Method method = spClz.getDeclaredMethod("get", String.class);
            String value = (String) method.invoke(spClz, property);
            if (TextUtils.isEmpty(value)) {
                return defaultValue;
            }
            return value;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}
